using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.ComponentModel;


public partial class ServiceProviderTypeEdt : BasePage
{
    SortedView serviceProviderTypesSorted;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ResetSortControls();
            SearchAndBind(int.Parse(tbPage.Text), tbSortDirection.Text, "");

            if (Request["Add"] != null)
                btnAddSPT_Click(sender, e);

        }

    }

    public void SearchAndBind(int _pageIndex, string sortDirection, string sortProperty, string commandName)
    {
        AdminServices.ServiceProviderTypeSearchRequest searchRequest = new AdminServices.ServiceProviderTypeSearchRequest();
        if (Request["SPTName"] != null)
            searchRequest.DisplayName = Request["SPTName"];
        if (Request["SPTCode"] != null)
            searchRequest.ServiceProviderTypeCode = Request["SPTCode"];
        searchRequest.InPool = true;

        AdminServices.AdminServices proxy = new AdminServices.AdminServices();
        AdminServices.ServiceProviderType[] serviceProviderTypes = proxy.ServiceProviderTypeSearch(CurrentPrincipal, searchRequest);
        serviceProviderTypesSorted = new SortedView(serviceProviderTypes);

        if (sortProperty != "")
        {
            ListSortDirection lsd;
            if (sortDirection == "ASC")
                lsd = ListSortDirection.Ascending;
            else
                lsd = ListSortDirection.Descending;
            serviceProviderTypesSorted.ApplySort(sortProperty, lsd);
        }

        Session.Add("serviceProviderTypesSorted", serviceProviderTypesSorted);

        gvServiceProviderTypes.DataSource = serviceProviderTypesSorted;
        gvServiceProviderTypes.PageIndex = _pageIndex;

        if (commandName != "EditSP")
            gvServiceProviderTypes.EditIndex = -1;

        gvServiceProviderTypes.DataBind();

    }

    private void SearchAndBind(int _pageIndex, string sortDirection, string sortProperty)
    {
        SearchAndBind(_pageIndex, sortDirection, sortProperty, "");
    }


    protected void gvServiceProviderTypes_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        SearchAndBind(e.NewPageIndex, tbSortDirection.Text, tbSortProperty.Text);
        
        tbPage.Text = e.NewPageIndex.ToString();
    }

    protected void gvServiceProviderTypes_Sorting(object sender, GridViewSortEventArgs e)
    {
        if (tbSortDirection.Text == "ASC" && tbSortProperty.Text == e.SortExpression)
            tbSortDirection.Text = "DESC";
        else
            tbSortDirection.Text = "ASC";

        tbSortProperty.Text = e.SortExpression;
        SearchAndBind(int.Parse(tbPage.Text), tbSortDirection.Text, e.SortExpression);
    }

    private void ResetSortControls()
    {
        tbPage.Text = "0";
        tbSortDirection.Text = "ASC";
        tbSortProperty.Text = "";
    }

    protected void btnAddSPT_Click(object sender, EventArgs e)
    {
        serviceProviderTypesSorted = (SortedView)Session["serviceProviderTypesSorted"];
        AdminServices.ServiceProviderType serviceProviderType = new AdminServices.ServiceProviderType(); 
        serviceProviderType = (AdminServices.ServiceProviderType)serviceProviderTypesSorted[0];

        if (serviceProviderType.ServiceProviderTypeID != -1)
        {
            AdminServices.ServiceProviderType[] serviceProviderTypeNew = new AdminServices.ServiceProviderType[serviceProviderTypesSorted.Count + 1];
            serviceProviderTypesSorted.CopyTo(serviceProviderTypeNew, 1);

            serviceProviderType = new AdminServices.ServiceProviderType();
            serviceProviderType.ServiceProviderTypeID = -1;
            serviceProviderType.DisplayName = "";
            serviceProviderType.ServiceProviderTypeCode = "";

            serviceProviderTypeNew[0] = serviceProviderType;

            serviceProviderTypesSorted = new SortedView(serviceProviderTypeNew);
        }

        Session.Add("serviceProviderTypesSorted", serviceProviderTypesSorted);

        gvServiceProviderTypes.EditIndex = 0;
        gvServiceProviderTypes.PageIndex = 0;
        gvServiceProviderTypes.DataSource = serviceProviderTypesSorted;
        gvServiceProviderTypes.DataBind();

    }

    protected void gvServiceProviderTypes_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        AdminServices.AdminServices proxy = new AdminServices.AdminServices();

        serviceProviderTypesSorted = (SortedView)Session["serviceProviderTypesSorted"];

        //unlock all
        proxy.ServiceProviderTypeUnlockAll(CurrentPrincipal);

        switch (e.CommandName)
        {
            case "EditSP":
                //lock record
                proxy.ServiceProviderTypeLock(CurrentPrincipal, Convert.ToInt32(gvServiceProviderTypes.Rows[Convert.ToInt32(e.CommandArgument)].Cells[0].Text)); //((AdminServices.ServiceProviderType)serviceProviderTypesSorted[int.Parse(e.CommandArgument.ToString())]).ServiceProviderTypeID);   
                //turn grid to edit mode
                gvServiceProviderTypes.EditIndex = int.Parse(e.CommandArgument.ToString());
                break;
            case "DeleteSP":
                //lock record
                proxy.ServiceProviderTypeLock(CurrentPrincipal, Convert.ToInt32(gvServiceProviderTypes.Rows[Convert.ToInt32(e.CommandArgument)].Cells[0].Text)); //((AdminServices.ServiceProviderType)serviceProviderTypesSorted[int.Parse(e.CommandArgument.ToString())]).ServiceProviderTypeID);
                //delete record
                proxy.ServiceProviderTypeDelete(CurrentPrincipal, Convert.ToInt32(gvServiceProviderTypes.Rows[Convert.ToInt32(e.CommandArgument)].Cells[0].Text)); //((AdminServices.ServiceProviderType)serviceProviderTypesSorted[int.Parse(e.CommandArgument.ToString())]).ServiceProviderTypeID);
                //unlock all
                proxy.ServiceProviderTypeUnlockAll(CurrentPrincipal);
                break;
            case "CancelSP":
                gvServiceProviderTypes.EditIndex = -1;
                break;
        }
        SearchAndBind(int.Parse(tbPage.Text), tbSortDirection.Text, tbSortProperty.Text, e.CommandName);
    }

    protected void btnSaveServiceProviderType_Click(object sender, EventArgs e)
    {
        int editIndex = gvServiceProviderTypes.EditIndex;
        if (editIndex > -1)
        {
            gvServiceProviderTypes.UpdateRow(editIndex, false);
        }
    }

    protected void gvServiceProviderType_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.Cells[0].Text == "-1")
        {
            e.Row.Cells[3].Controls[0].Visible = false;
            e.Row.Cells[4].Controls[0].Visible = false;
            e.Row.Cells[0].Text = "";
        }
    }

    protected void gvServiceProviderType_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string fDisplayName = Utilities.getCellValue((GridView)sender, e, 1,0);
        string fServiceProviderTypeCode = Utilities.getCellValue((GridView)sender, e, 2,1);        

        AdminServices.AdminServices proxy = new AdminServices.AdminServices();

        int editIndex = ((GridView)sender).EditIndex;
        editIndex = editIndex + (gvServiceProviderTypes.PageIndex * gvServiceProviderTypes.PageSize);

        serviceProviderTypesSorted = (SortedView)Session["serviceProviderTypesSorted"];
        AdminServices.ServiceProviderType serviceProviderTypeEdit = (AdminServices.ServiceProviderType)serviceProviderTypesSorted[editIndex];
        

        serviceProviderTypeEdit.DisplayName = fDisplayName;
        serviceProviderTypeEdit.ServiceProviderTypeCode = fServiceProviderTypeCode;

        if (serviceProviderTypeEdit.ServiceProviderTypeID > -1)
        {
            //Editted record
            proxy.ServiceProviderTypeUpdate(CurrentPrincipal, serviceProviderTypeEdit);
        }
        else
        {
            //insert record
            proxy.ServiceProviderTypeInsert(CurrentPrincipal, serviceProviderTypeEdit);
        }

        //unlock all
        proxy.ServiceProviderTypeUnlockAll(CurrentPrincipal);

        gvServiceProviderTypes.EditIndex = -1;

        SearchAndBind(int.Parse(tbPage.Text), tbSortDirection.Text, tbSortProperty.Text);

    }

    protected void gvServiceProviderTypes_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.Cells[0].Text == "-1")
        {
            e.Row.Cells[3].Controls[0].Visible = false;
            e.Row.Cells[4].Controls[0].Visible = false;
            e.Row.Cells[0].Text = "";
        }
    }
}
